package com.servlet.request; /**
 * @author wzm
 * @create 2023-06-21 9:29
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/rs1")
public class RequestServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //测试request对象获取请求相关信息
        //1.获取请求行数据
        //①获取当前请求的方式
        String method = request.getMethod();//GET
        System.out.println("method = " + method);
        
        //②获取项目路径（虚拟目录 项目运行的上下文）
        String contextPath = request.getContextPath();// /javaweb05
        System.out.println("contextPath = " + contextPath);
        
        //③获取项目的URL(同一资源定位符)
        StringBuffer requestURL = request.getRequestURL();//http://localhost:8080/web05/rs1
        System.out.println("requestURL.toString() = " + requestURL.toString());
        
        //④获取项目的URI(统一资源标识符)
        String requestURI = request.getRequestURI();///web05/rs1
        System.out.println("requestURI = " + requestURI);
        
        //⑤获取请求参数（Get）
        String queryString = request.getQueryString();
        System.out.println("queryString = " + queryString);

        //2.获取请求头 （除了有需要否则将毫无意义） 根据key获取value
        String header = request.getHeader("User-Agent");
        System.out.println("header = " + header);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到post请求的参数
        //对于post请求 可以传入普通数据  也可以是文件上传数据
        
        //1.获取字符输入流 基本的用户名  密码
        BufferedReader reader = request.getReader();  
        //2.读取流中的数据
        String s = reader.readLine();
        System.out.println("s = " + s);


        //如果发送过来的是文件 字节数据
//        ServletInputStream inputStream = request.getInputStream();
//        int read = inputStream.read();

    }
}
